package main

import (
	"fmt"
	"github.com/swaggest/openapi-go/openapi3"
	"log"
	"net/http"
	"os"
	"path"
)

// Fatal 简化错误判断 抛出异常
func Fatal(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func main() {
	reflector := openapi3.Reflector{}
	reflector.Spec = &openapi3.Spec{Openapi: "3.0.3"}
	reflector.Spec.Info.
		WithTitle("神达NFT项目 API").
		WithVersion("0.0.2").
		WithDescription("神达NFT项目 API 文档 使用openapi3")

	serverLocal := openapi3.Server{}
	serverLocal.WithURL("http://127.0.0.1:8080")
	serverLocal.WithDescription("本地测试环境")

	serverCloud := openapi3.Server{}
	serverCloud.WithURL("http://aqchain_web.xyz:8080")
	serverCloud.WithDescription("腾讯云测试环境")

	reflector.Spec.WithServers(serverLocal, serverCloud)

	operation := openapi3.Operation{}
	operation.WithTags("contract")
	operation.WithSummary("部署合约")
	operation.WithDescription("部署合约")
	operation.WithID("contract-deploy")
	Fatal(reflector.SetRequest(&operation, new(DeployRequest), http.MethodPost))
	Fatal(reflector.SetJSONResponse(&operation, new(DeployResponse), http.StatusOK))
	Fatal(reflector.SetJSONResponse(&operation, new(ErrorResponse), http.StatusBadRequest))
	Fatal(reflector.SetJSONResponse(&operation, new(ErrorResponse), http.StatusInternalServerError))
	Fatal(reflector.Spec.AddOperation(http.MethodPost, "/api/contract/deploy", operation))

	operation = openapi3.Operation{}
	operation.WithTags("contract")
	operation.WithSummary("批量部署合约")
	operation.WithDescription("批量部署合约")
	operation.WithID("contract-deployBatch")
	Fatal(reflector.SetRequest(&operation, new(DeployBatchRequest), http.MethodPost))
	Fatal(reflector.SetJSONResponse(&operation, new(DeployBatchResponse), http.StatusOK))
	Fatal(reflector.SetJSONResponse(&operation, new(ErrorResponse), http.StatusBadRequest))
	Fatal(reflector.SetJSONResponse(&operation, new(ErrorResponse), http.StatusInternalServerError))
	Fatal(reflector.Spec.AddOperation(http.MethodPost, "/api/contract/deployBatch", operation))

	operation = openapi3.Operation{}
	operation.WithTags("contract")
	operation.WithSummary("铸造NFT")
	operation.WithDescription("铸造NFT")
	operation.WithID("contract-mint")
	Fatal(reflector.SetRequest(&operation, new(MintRequest), http.MethodPost))
	Fatal(reflector.SetJSONResponse(&operation, new(MintResponse), http.StatusOK))
	Fatal(reflector.SetJSONResponse(&operation, new(ErrorResponse), http.StatusBadRequest))
	Fatal(reflector.SetJSONResponse(&operation, new(ErrorResponse), http.StatusInternalServerError))
	Fatal(reflector.Spec.AddOperation(http.MethodPost, "/api/contract/mint", operation))

	operation = openapi3.Operation{}
	operation.WithTags("contract")
	operation.WithSummary("批量铸造NFT")
	operation.WithDescription("批量铸造NFT")
	operation.WithID("contract-mintBatch")
	Fatal(reflector.SetRequest(&operation, new(MintBatchRequest), http.MethodPost))
	Fatal(reflector.SetJSONResponse(&operation, new(MintBatchResponse), http.StatusOK))
	Fatal(reflector.SetJSONResponse(&operation, new(ErrorResponse), http.StatusBadRequest))
	Fatal(reflector.SetJSONResponse(&operation, new(ErrorResponse), http.StatusInternalServerError))
	Fatal(reflector.Spec.AddOperation(http.MethodPost, "/api/contract/mintBatch", operation))

	schema, err := reflector.Spec.MarshalJSON()
	if err != nil {
		log.Fatal(err)
	}

	wd, err := os.Getwd()
	if err != nil {
		return
	}
	Fatal(os.WriteFile(path.Join(wd, "openapi.json"), schema, 0777))
	fmt.Println(string(schema))
}
